/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sbmart.smartserver.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sbmart.object.*;
import sbmart.smartserver.cache.ConnectionManager;

/**
 *
 * @author albert
 */
public class ClientTrsService {

    private CallableStatement call_put_mst_rent_terrace;
    private CallableStatement call_put_trs_kirim_mst;
    private CallableStatement call_put_trs_kirim_detail;
    private CallableStatement call_put_trs_kirimp_mst;
    private CallableStatement call_put_trs_kirimp_detail;
    private CallableStatement call_put_tr_mutasi_dest;
    private CallableStatement call_put_tr_mutasi_src;
    private static ClientTrsService instance;
    public static final Logger LOG = LoggerFactory.getLogger("ServerTrs");

    private ClientTrsService() {
    }

    public static ClientTrsService instance() {
        if (instance == null) {
            instance = new ClientTrsService();
            instance.init();
        }
        return instance;
    }

    public void init() {
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            LOG.debug("[ERROR]", ex);
        }
    }

    public Connection putTrsRentTerrace(Header header, List<TrsRentTerrace> data, Connection conn) throws SQLException {
        LOG.debug("[PUT]");
        call_put_mst_rent_terrace = conn.prepareCall(ClientProcedures.put_trs_rent_terrace);
        for (TrsRentTerrace mst : data) {
            call_put_mst_rent_terrace.setInt("rent_id", mst.getRentId());
            call_put_mst_rent_terrace.setString("trans_num", mst.getTransNum());
            if (mst.getTransDate() != null) {
                call_put_mst_rent_terrace.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_mst_rent_terrace.setDate("trans_date", null);
            }
            call_put_mst_rent_terrace.setInt("branch_id", mst.getBranchId());
            call_put_mst_rent_terrace.setInt("relation_id", mst.getRelationId());
            call_put_mst_rent_terrace.setString("area", mst.getArea());
            call_put_mst_rent_terrace.setInt("location", mst.getLocation());
            call_put_mst_rent_terrace.setInt("cart", mst.getCart());
            call_put_mst_rent_terrace.setInt("electric_quota", mst.getElectricQuota());
            call_put_mst_rent_terrace.setInt("conduit", mst.getConduit());
            if (mst.getStartDate() != null) {
                call_put_mst_rent_terrace.setDate("start_date", new java.sql.Date(mst.getStartDate().getTimeInMillis()));
            } else {
                call_put_mst_rent_terrace.setDate("start_date", null);
            }
            if (mst.getEndDate() != null) {
                call_put_mst_rent_terrace.setDate("end_date", new java.sql.Date(mst.getEndDate().getTimeInMillis()));
            } else {

                call_put_mst_rent_terrace.setDate("end_date", null);
            }
            call_put_mst_rent_terrace.setInt("termin", mst.getTermin());
            call_put_mst_rent_terrace.setFloat("price", mst.getPrice());
            call_put_mst_rent_terrace.setInt("pay_type", mst.getPayType());
            call_put_mst_rent_terrace.setString("product", mst.getProduct());
            call_put_mst_rent_terrace.setFloat("cart_price", mst.getCartPrice());
            call_put_mst_rent_terrace.setFloat("subtotal", mst.getSubtotal());
            call_put_mst_rent_terrace.setFloat("ppn", mst.getPpn());
            call_put_mst_rent_terrace.setFloat("total", mst.getTotal());
            call_put_mst_rent_terrace.setInt("cart_type", mst.getCartType());
            call_put_mst_rent_terrace.setString("product2", mst.getProduct2());
            call_put_mst_rent_terrace.setInt("dimensi", mst.getDimensi());
            call_put_mst_rent_terrace.setString("notes", mst.getNotes());
            call_put_mst_rent_terrace.setString("first_party", mst.getFirstParty());
            call_put_mst_rent_terrace.setString("insert_log", mst.getInsertLog());
            call_put_mst_rent_terrace.setString("edit_log", mst.getEditLog());
            call_put_mst_rent_terrace.setString("first_party_address", mst.getFirstPartyAddress());
            call_put_mst_rent_terrace.setShort("lead_time", mst.getLeadTime());
            call_put_mst_rent_terrace.addBatch();
        }
        ConnectionManager.instance().isOpen(conn);
        call_put_mst_rent_terrace.executeBatch();
        return conn;
    }

    public Connection putTrsKirim(Header header, List<TrsKirimMst> data, Connection conn) throws SQLException {
        LOG.debug("[PUT]");
        call_put_trs_kirim_mst = conn.prepareCall(ClientProcedures.put_trs_kirim_mst);
        call_put_trs_kirim_detail = conn.prepareCall(ClientProcedures.put_trs_kirim_detail);
        for (TrsKirimMst mst : data) {
            call_put_trs_kirim_mst.setInt("trans_id", mst.getTransId());

            if (mst.getTransDate() != null) {
                call_put_trs_kirim_mst.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
                call_put_trs_kirim_mst.setDate("delivery_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_kirim_mst.setDate("trans_date", null);
                call_put_trs_kirim_mst.setDate("delivery_date", null);
            }
            call_put_trs_kirim_mst.setInt("branch_id", mst.getBranchId());
            call_put_trs_kirim_mst.setString("trans_num", mst.getTransNum());
            call_put_trs_kirim_mst.setString("delivery_num", mst.getTransNum());
            call_put_trs_kirim_mst.setString("no_pol", mst.getNopol());
            call_put_trs_kirim_mst.setString("users_id", mst.getUserId());
            call_put_trs_kirim_mst.setString("serial_keys", mst.getSerialKey());
            call_put_trs_kirim_mst.setString("pass_keys", mst.getPassKey());
            call_put_trs_kirim_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_kirim_mst.setFloat("tax", mst.getTax());
            call_put_trs_kirim_mst.setInt("np_type", 0);
            call_put_trs_kirim_mst.setFloat("total", mst.getTotal());
            call_put_trs_kirim_mst.setString("pb_num", mst.getPbNum());

            call_put_trs_kirim_mst.registerOutParameter(1, Types.INTEGER);
            ConnectionManager.instance().isOpen(conn);
            call_put_trs_kirim_mst.execute();
            int transId = call_put_trs_kirim_mst.getInt(1);

            if (transId != 0) {
                List<TrsKirimDetail> details = mst.getDetails();
                for (TrsKirimDetail detail : details) {
                    call_put_trs_kirim_detail.setInt("terima_gerai_id", transId);
                    call_put_trs_kirim_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_kirim_detail.setFloat("quantity", detail.getQuantity());
                    call_put_trs_kirim_detail.setFloat("qty_req", detail.getQtyOrder());
                    call_put_trs_kirim_detail.setFloat("price", detail.getPrice());
                    call_put_trs_kirim_detail.setFloat("tax", detail.getTax());
                    call_put_trs_kirim_detail.setFloat("amount", detail.getAmount());
                    call_put_trs_kirim_detail.setInt("is_tax", detail.getIsTax());
                    call_put_trs_kirim_detail.setString("packaging", detail.getPackaging());
                    call_put_trs_kirim_detail.addBatch();
                }
                ConnectionManager.instance().isOpen(conn);
                call_put_trs_kirim_detail.executeBatch();
            }
        }
        return conn;

    }

    public Connection putTrsKirimP(Header header, List<TrsKirimpMst> data, Connection conn) throws SQLException {
        LOG.debug("[PUT] ");
        call_put_trs_kirimp_mst = conn.prepareCall(ClientProcedures.put_trs_kirimp_mst);
        call_put_trs_kirimp_detail = conn.prepareCall(ClientProcedures.put_trs_kirimp_detail);
        for (TrsKirimpMst mst : data) {
            call_put_trs_kirimp_mst.setInt("trans_id", mst.getTransId());

            if (mst.getTransDate() != null) {
                call_put_trs_kirimp_mst.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
                call_put_trs_kirimp_mst.setDate("delivery_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_kirimp_mst.setDate("trans_date", null);
                call_put_trs_kirimp_mst.setDate("delivery_date", null);
            }
            call_put_trs_kirimp_mst.setInt("branch_id", mst.getBranchId());
            call_put_trs_kirimp_mst.setString("trans_num", mst.getTransNum());
            call_put_trs_kirimp_mst.setString("delivery_num", mst.getTransNum());
            call_put_trs_kirimp_mst.setString("no_pol", mst.getNopol());
            call_put_trs_kirimp_mst.setString("users_id", mst.getUserId());
            call_put_trs_kirimp_mst.setString("serial_keys", mst.getSerialKey());
            call_put_trs_kirimp_mst.setString("pass_keys", mst.getPassKey());
            call_put_trs_kirimp_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_kirimp_mst.setFloat("tax", mst.getTax());
            call_put_trs_kirimp_mst.setInt("np_type", mst.getTransType());
            call_put_trs_kirimp_mst.setFloat("total", mst.getTotal());
            call_put_trs_kirimp_mst.setString("pb_num", mst.getPbNum());

            call_put_trs_kirimp_mst.registerOutParameter(1, Types.INTEGER);
            ConnectionManager.instance().isOpen(conn);
            call_put_trs_kirimp_mst.execute();
            int transId = call_put_trs_kirimp_mst.getInt(1);

            List<TrsKirimpDetail> details = mst.getDetails();
            for (TrsKirimpDetail detail : details) {
                call_put_trs_kirimp_detail.setInt("terima_gerai_id", transId);
                call_put_trs_kirimp_detail.setInt("item_id", detail.getItemId());
                call_put_trs_kirimp_detail.setFloat("quantity", detail.getQuantity());
                call_put_trs_kirimp_detail.setFloat("qty_req", detail.getQtyOrder());
                call_put_trs_kirimp_detail.setFloat("price", detail.getPrice());
                call_put_trs_kirimp_detail.setFloat("tax", detail.getTax());
                call_put_trs_kirimp_detail.setFloat("amount", detail.getAmount());
                call_put_trs_kirimp_detail.setInt("is_tax", detail.getIsTax());
                call_put_trs_kirimp_detail.setString("packaging", detail.getPackaging());
                call_put_trs_kirimp_detail.addBatch();
            }
            ConnectionManager.instance().isOpen(conn);
            call_put_trs_kirimp_detail.executeBatch();
        }
        return conn;
    }

    public Connection putTrMutasiDest(Header header, List<TrMutasi> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_tr_mutasi_dest = clientConn.prepareCall(ClientProcedures.put_tr_mutasi_dest);
        for (TrMutasi mst : data) {
            call_put_tr_mutasi_dest.setString("nik", mst.getNik());
            call_put_tr_mutasi_dest.setString("nama", mst.getNama());
            call_put_tr_mutasi_dest.setString("alamat", mst.getAlamat());
            call_put_tr_mutasi_dest.setString("telp", mst.getTelp());
            call_put_tr_mutasi_dest.setString("dest_stkary", mst.getDestStkary());
            call_put_tr_mutasi_dest.setString("accept", mst.getAccept());
            call_put_tr_mutasi_dest.setString("acceptor", mst.getAcceptor());
            call_put_tr_mutasi_dest.setString("posisi", mst.getPosisi());
            
            ConnectionManager.instance().isOpen(clientConn);
            call_put_tr_mutasi_dest.execute();
        }
        return clientConn;
    }

    public Connection putTrMutasiSrc(Header header, List<TrMutasi> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_tr_mutasi_src = clientConn.prepareCall(ClientProcedures.put_tr_mutasi_src);
        for (TrMutasi mst : data) {
            call_put_tr_mutasi_src.setString("nik", mst.getNik());
            call_put_tr_mutasi_src.setString("src_stkary", mst.getSrcStkary());
            
            ConnectionManager.instance().isOpen(clientConn);
            call_put_tr_mutasi_src.execute();
        }
        return clientConn;
    }
}
